#include "stdafx.h"
#include "Claxa.h"
#include "MainFrame.h"
#include <mswin/module.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// Claxa

BEGIN_MESSAGE_MAP(Claxa, Claxa::Base)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
	ON_COMMAND(ID_CACHE_SETTINGS, OnCacheSettings)
	ON_COMMAND(ID_RENDERING_SETTINGS, OnRenderingSettings)
	ON_COMMAND(ID_LANGUAGE_ENGLISH, OnLanguageEnglish)
	ON_UPDATE_COMMAND_UI(ID_LANGUAGE_ENGLISH, OnUpdateLanguageEnglish)
	ON_COMMAND(ID_LANGUAGE_RUSSIAN, OnLanguageRussian)
	ON_UPDATE_COMMAND_UI(ID_LANGUAGE_RUSSIAN, OnUpdateLanguageRussian)
END_MESSAGE_MAP()


// Claxa construction

Claxa::Claxa() :
	m_RenderSettings(true)
{
//	m_hInstResDLL = 0;
}

// The one and only Claxa object

Claxa theApp;

// Claxa initialization

BOOL Claxa::InitInstance()
{
//	m_Language = (Language)GetProfileInt("Preferences", "Language", English);
//	if ((unsigned)m_Language > Russian)
//		m_Language = English;

//	LoadLanguageResourseDll();

	InitCommonControls();
	SetRegistryKey("Pixmart");

	//m_Language.Init("Preferences", "Language");

	EnableLanguage(My::LANG_ENU);
	EnableLanguage(My::LANG_RUS);
	SetDefaultLanguage(My::LANG_ENU);

	InitLanguage("Preferences", "Language");

	if (!Base::InitInstance())
		return FALSE;

	return TRUE;
}

int Claxa::ExitInstance()
{
//	theApp.WriteProfileInt("Preferences", "Language", m_Language);

//	FreeLibrary(m_hInstResDLL);
	return Base::ExitInstance();
}

// Claxa message handlers



// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
public:
	virtual BOOL OnInitDialog();
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BOOL CAboutDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	SetDlgItemText(IDC_BUILT_ON, mswin::exe_mod_date().c_str());

	return TRUE;  // return TRUE unless you set the focus to a control
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()

// App command to run the dialog
void Claxa::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

void Claxa::OnCacheSettings()
{
	m_Cache.modaless_configure_show(m_pMainWnd->m_hWnd);
}

void Claxa::OnRenderingSettings()
{
	m_RenderSettings.configure(true, m_pMainWnd->m_hWnd);
}

void Claxa::LoadLanguageResourseDll()
{
/*	if (m_hInstResDLL)
		FreeLibrary(m_hInstResDLL);
	switch (m_Language) {
	case English:
		#ifdef _DEBUG
			m_hInstResDLL = LoadLibrary("ClaxaResEnuD.dll");
		#else
			m_hInstResDLL = LoadLibrary("ClaxaResEnu.dll");
		#endif
		break;
	case Russian:
		#ifdef _DEBUG
			m_hInstResDLL = LoadLibrary("ClaxaResRusD.dll");
		#else
			m_hInstResDLL = LoadLibrary("ClaxaResRus.dll");
		#endif
		break;
	default:
		ASSERT(FALSE);
	}
	ASSERT (m_hInstResDLL != NULL);
*/
}

void Claxa::SetLanguage(My::Language)
{
/*	if (m_Language != l) {
		m_Language = l;
		LoadLanguageResourseDll();
	}
*/
}

void Claxa::OnLanguageEnglish()
{
//	SetLanguage(English);
}

void Claxa::OnUpdateLanguageEnglish(CCmdUI *pCmdUI)
{
///	pCmdUI->SetRadio(m_Language == English);
}

void Claxa::OnLanguageRussian()
{
//	SetLanguage(Russian);
}

void Claxa::OnUpdateLanguageRussian(CCmdUI *pCmdUI)
{
//	pCmdUI->SetRadio(m_Language == Russian);
}

bool IsEditWindow(HWND hWnd) 
{
	WINDOWINFO info;
	GetWindowInfo(hWnd, &info);
	WNDCLASS cls1;
	GetClassInfo(NULL, MAKEINTATOM(info.atomWindowType), &cls1);

	WNDCLASS cls2;
	GetClassInfo(NULL, "EDIT", &cls2);
	return cls1.lpfnWndProc == cls2.lpfnWndProc;
}

BOOL Claxa::PreTranslateMessage(MSG* pMsg)
{
	if (pMsg->message == WM_KEYDOWN && 
		GetFrame().IsWindowEnabled() && //prevent keys blocking in modal dialogs
		IsEditWindow(pMsg->hwnd)) 
	{
		return CWnd::FromHandle(pMsg->hwnd)->PreTranslateMessage(pMsg);
	}
	return Base::PreTranslateMessage(pMsg);
}
